package com.zxw.common.web.util;

import cn.hutool.core.util.StrUtil;
import com.zxw.common.core.base.enums.EnumBeanNameEnum;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Configuration;

import javax.annotation.Resource;
import javax.validation.constraints.NotNull;
import java.util.Map;
import java.util.Objects;

/**
 * @author Xuewei.Zhang
 * @date 2022/7/5
 */
@Configuration
public class BeanUtils {

    private static ApplicationContext applicationContext;

    @Resource
    public void setApplicationContext(ApplicationContext applicationContext) {
        BeanUtils.applicationContext = applicationContext;
    }

    public static Integer getCodeByMsg(String msg, @NotNull EnumBeanNameEnum beanNameEnum) {
        Map<String, Integer> map = (Map<String, Integer>) applicationContext.getBean(beanNameEnum.getMsg());
        return map.get(msg);
    }

    public static String getMsgByCode(Integer code, @NotNull EnumBeanNameEnum beanNameEnum) {
        Map<String, Integer> map = (Map<String, Integer>) applicationContext.getBean(beanNameEnum.getMsg());
        final String[] msg = {""};
        map.forEach((k, v) -> {
            if(code.equals(v)){
                msg[0] = k;
                return;
            }
        });
        return msg[0];
    }
}
